package com.taotao.common;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 前端分页数据模型
 *
 * @author fanbeibei
 * @date 2020/10/2 15:43
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Pagination<T, P> {
    @ApiModelProperty(value = "当前页号,从1开始")
    private int pageNum;
    @ApiModelProperty(value = "每页的数量")
    private int pageSize;
    @ApiModelProperty(value = "总页数")
    private int pages;
    @ApiModelProperty(value = "总记录数")
    protected long total;
    @ApiModelProperty(value = "结果集")
    protected List<T> list;

    @ApiModelProperty(value = "前端查询条件")
    protected P param;


    /**
     * 包装Page对象
     *
     * @param page page结果
     */
    public static <T> Pagination<T, ?> fromPage(Page<T> page) {
        Pagination<T, ?> p = new Pagination<>();

        p.pageNum = page.getPageNum();
        p.pageSize = page.getPageSize();
        p.pages = page.getPages();
        p.total = page.getTotal();
        p.list = page.getResult();
        return p;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
